package com.customerlogin.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.customerlogin.entity.FileCustomer;
import com.entity.Customer;
import com.interfaces.CustomerDao;
import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: CustomerLoginService.java
 * @Package com.customerlogin.service
 * @author 孙梦楠
 * @date 2017年10月25日 下午3:50:15
 * @version V1.0.0
 */
@Service
public class CustomerLoginService {
	
	
	private static final Logger log = LoggerFactory.getLogger(CustomerLoginService.class);


	@Autowired
	private CustomerDao customerdao;
	
	public static final String appid="wxc101431802d2a529";
	
	public static final String secret="9cdb8fe0c84696260d87f38dce2d2313";
	
	
	
	
	// 登录，绑定判断
	@Transactional
	public JsonResult<List<FileCustomer>> login(String wechatname,String code) {
		//获取openid
		String openid = findopenid(appid, secret, code);
		
		if (customerdao.findByOpenid(openid).size() == 0) {
			return new JsonResult<List<FileCustomer>>(1, "尚未绑定账户，请绑定", null);
		} else {
			List<FileCustomer> fileCustomers = new ArrayList<FileCustomer>();
			List<Customer> customers = customerdao.findByOpenid(openid);
			for (Customer customer : customers) {
				FileCustomer fileCustomer = new FileCustomer();
				CglibBeanCopierUtils.copyProperties(customer, fileCustomer);
				fileCustomers.add(fileCustomer);
			}
			return new JsonResult<List<FileCustomer>>(2, "展示所有绑定的户号", fileCustomers);
		}

	}

	// 绑定户号
	@Transactional
	public JsonResult<String> bind(String customeraccount, String wechatname,String code) {
		
		//获取openid
				String openid = findopenid(appid, secret, code);
		// 为绑定过且户号户名对应
		if (customerdao.findByCustomeraccount(customeraccount)!=null &&
				Utils.isNullOrEmpty(customerdao.findByCustomeraccount(customeraccount).getCustomerwechat())) {
			customerdao.updateCustomerwechat(wechatname, openid,customeraccount);
			log.info("bind success  "+customeraccount);
			return new JsonResult<String>(3, "绑定成功", null);
		} else {
			return new JsonResult<String>(4, "绑定失败", null);
		}

	}

//	// 解绑户号
//	@Transactional
//	public JsonResult<String> unbind(String customeraccount, String customerwechat) {
//		if (customerdao.findByCustomeraccount(customeraccount)!=null &&
//				Utils.notNullAndEmpty(customerdao.findByCustomeraccount(customeraccount).getCustomerwechat())) {
//			customerdao.updateCustomerwechat(null, customeraccount);
//			return new JsonResult<String>(5, "解绑成功", null);
//		} else {
//			return new JsonResult<String>(6, "解绑失败", null);
//		}
//
//	}

	@Transactional
	public JsonResult<String> test() {
		return new JsonResult<String>(6, "test", "test");

	}
	
	//通过API查到openid
	public String findopenid(String appid,String secret,String code) {
		
		String openid = "111";
		
		return openid;
	}
	

}
